﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLNHBUS;
using QLNHDAO;

namespace PMQLNhaHang
{
    public partial class fmQLNguoiDungNhanVien : Form
    {
        private int _maNhanVien;
        private NhanVien _nhanVien;
        public fmQLNguoiDungNhanVien()
        {
            InitializeComponent();
            _maNhanVien = -1;
        }
        private void fmQLNguoiDungNhanVien_Load(object sender, EventArgs e)
        {
            //quản lý sửa thông tin nhân viên
            if (_maNhanVien != -1)
            {
                _nhanVien = NhanVienBUS.LayNhanVienTheoMa(2);
                LoadNhanVien(_nhanVien);
            }
            //thu ngân cập nhật thông tin
            if(!KiemTraQuyenHan())
            {
                LoadNhanVien(fmLogin.NhanVienInstance);
            }
            //quản lý thêm mới nhân viên tự load
        }
        private void ptbHinhAnh_Click(object sender, EventArgs e)
        {
            string strimage = null;
            openFileDialog1.Title = "Chọn Hình Ảnh";
            openFileDialog1.ShowDialog();
            strimage = openFileDialog1.FileName;
            if (!string.IsNullOrEmpty(strimage))
            {
                ptbHinhAnh.Image = LoadImgeFromFile(strimage);
            }
        }
        private void btnLuu_Click(object sender, EventArgs e)
        {
            if ((_maNhanVien == -1)&&KiemTraQuyenHan())
            {
                NhanVien nv = new NhanVien();
                TaoNhanVien(nv);
                NhanVienBUS.ThemNhanVien(nv);
            }
            if(_maNhanVien!=-1)
            {
                TaoNhanVien(_nhanVien);
                NhanVienBUS.CapNhatXoaNhanVien(_nhanVien);
            }
            if(!KiemTraQuyenHan())
            {
                NhanVien nv = fmLogin.NhanVienInstance;
                TaoNhanVien(nv);
                NhanVienBUS.CapNhatXoaNhanVien(nv);
                fmLogin.NhanVienInstance = nv;
            }
            this.Hide();
        }
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
        #region Method

        public void LoadNhanVien(NhanVien nv)
        {
            tbHoTen.Text = nv.HoTen;
            dtpNgaySinh.Value = (DateTime) nv.NgaySinh;
            tbSoDienThoai.Text = nv.DienThoai;
            tbDiaChi.Text = nv.DiaChi;
            tbCMND.Text = nv.CMND;
            tbTenDangNhap.Text = nv.TenDangNhap;
            tbMatKhau.Text = nv.MatKhau;
            if (nv.Loai == 0)
            {
                cbChucVu.SelectedIndex = 0;
                cbChucVu.Enabled = false;
            }
            else
            {
                cbChucVu.SelectedIndex = 1;
            }
            if (nv.Hinh != null)
            {
                Byte[] hinhAnh = nv.Hinh.ToArray();
                Image avatar = ConvertByteToImage(hinhAnh);
                ptbHinhAnh.Image = avatar;
            }
        }
        private void TaoNhanVien(NhanVien nv)
        {
            nv.HoTen = tbHoTen.Text.Trim();
            nv.NgaySinh = dtpNgaySinh.Value;
            nv.DienThoai = tbSoDienThoai.Text.Trim();
            nv.DiaChi = tbDiaChi.Text.Trim();
            nv.CMND = tbCMND.Text.Trim();
            nv.Loai = cbChucVu.SelectedIndex;
            nv.TenDangNhap = tbTenDangNhap.Text;
            nv.MatKhau = tbMatKhau.Text;
            nv.Xoa = false;
            if (ptbHinhAnh.Image != null)
            {
                nv.Hinh = ConvertImageToByte(ptbHinhAnh.Image);
            }
        }

        public static  byte[]ConvertImageToByte(Image img)
        {
            MemoryStream ms = new MemoryStream();
            img.Save(ms,ImageFormat.Jpeg);
            return ms.ToArray();
        }
        public static Image ConvertByteToImage(Byte[] bai)
        {
            MemoryStream bit = new MemoryStream(bai);
            Image img = new Bitmap(bit);
            return img;
        }
        public  static Image LoadImgeFromFile(string str)
        {
            Image img;
            Byte[] bimage = LoadByteImageFromFile(str);
            img = ConvertByteToImage(bimage);
            return img;
        }
        public  static  Byte[] LoadByteImageFromFile(string str)
        {
            FileStream fsimage = File.OpenRead(str);
            byte[] bimage = new byte[fsimage.Length];
            fsimage.Read(bimage, 0, bimage.Length);
            return bimage;
        }
        public static  bool KiemTraQuyenHan()
        {
            if (fmLogin.NhanVienInstance.Loai == 1)
                return true;
            return false;
        }
        public void NhanMaNhanVien(int maNhanVien)
        {
            this._maNhanVien = maNhanVien;
        }
        #endregion
    }
}
